package com.jinhua.admin.controller;

import cn.common.dto.RetryRequestParamDTO;
import com.jinhua.admin.service.UserService;
import com.jinhua.admin.vo.UserVo;
import com.jinhua.config.IdUtil;
import com.jinhua.config.PaginationRequest;
import com.jinhua.config.Response;
import com.util.config.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户会员表的操作接口
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 添加或者修改用户会员表
     *
     * @return
     */
    @PostMapping("save")
    public Response saveUser(@Valid @RequestBody UserVo userVo) {
        if (userVo == null) {
            return Response.error();
        }
        return Response.success(userService.saveUser(userVo));
    }

    /**
     * 删除用户会员表
     *
     * @param ids 用户会员表的Id的集合,例如1,2,3多个用英文,隔开
     * @return
     */
    @RequestMapping("del")
    public Response delUser(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Response.error();
        }
        return Response.success(userService.delUserByIds(ids));
    }

    /**
     * 查找用户会员表
     *
     * @return
     */
    @RequestMapping("find")
    public Response findUser(@RequestBody @Valid PaginationRequest<UserVo> request) {
        if (request == null) {
            return Response.error();
        }
        UserVo userVo = request.getQueryCriteria();
        if (userVo.getId() != null) {
            return Response.success(userService.findUserById(userVo.getId()));
        }
        return Response.success(userService.findUserByAll(request), IdUtil.getTotal());
    }
    @PostMapping("/doPostReq")
    public HttpResult<String> doPostReq(@RequestBody UserVo req) {
        try {
//            if (!checkGreenWhiteList(req)) {
//                return HttpResult.fail("非green白名单");
//            }
            RetryRequestParamDTO<UserVo> retryRequest = new RetryRequestParamDTO<>();
            retryRequest.setRequestBody(req);
            HttpResult<String> postReqReuslt = userService.doWork(retryRequest);
            return HttpResult.succ(postReqReuslt.getData());
        } catch (Exception e) {
            return HttpResult.fail(e.getMessage());
        }
    }
}
